package cn.yangliu.springbatch.job;

import org.springframework.batch.item.database.orm.AbstractJpaQueryProvider;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.persistence.Query;

/**
 * the type of springbatch
 *
 * <p>
 * .
 *
 * @author 杨柳
 * @date 2021-07-12
 */
public class JpaJpqlQueryProvider<E> extends AbstractJpaQueryProvider {

    private Class<E> entityClass;
    private String jpqlQuery;

    public JpaJpqlQueryProvider() {
    }
    public JpaJpqlQueryProvider(Class<E> entityClass, String jpqlQuery) {
        this.entityClass = entityClass;
        this.jpqlQuery = jpqlQuery;
    }

    @Override
    public Query createQuery() {
        return this.getEntityManager().createQuery(this.jpqlQuery, this.entityClass);
    }

    public void setJpqlQuery(String jpqlQuery) {
        this.jpqlQuery = jpqlQuery;
    }

    public void setEntityClass(Class<E> entityClazz) {
        this.entityClass = entityClazz;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.isTrue(StringUtils.hasText(this.jpqlQuery), "JPQL query cannot be empty");
        Assert.notNull(this.entityClass, "Entity class cannot be NULL");
    }
}
